var memedia_id_temp, nickname_temp, uuid_temp;
var login_status = false;

function getTask(uid) {
	var result;
	try {
		if (uid) {
			var getTaskUrl = 'http://memedia.oaqi.com/auth/getWaitAuth?int800_user=' + uid;
			sendRequest(getTaskUrl, '', 'GET', false, function(response) {
				result = response;
			});
		}
	} catch (e) {
	}
	return result;
}

function login(memedia_id, nickname, uuid) {
	memedia_id_temp = memedia_id;
	nickname_temp = nickname;
	uuid_temp = uuid;
	login_status = true;
}

function upUUID(uuid) {
	uuid = uuid ? uuid : uuid_temp;
	var setCookiesUrl ='http://memedia.oaqi.com/auth/setUUID';
	sendRequest(setCookiesUrl, `id=${memedia_id_temp}&uuid=${uuid}`, 'POST', false, function(response) {
		console.log(response);
	});
	login_status = false;
	memedia_id_temp = null;
	nickname_temp = null;
	uuid_temp = null;
}

async function upCookies(url) {
	var cookies = await getCookies(url);
	cookies = JSON.stringify(cookies);
	var setCookiesUrl ='http://memedia.oaqi.com/auth/setCookies';
	sendRequest(setCookiesUrl, `id=${memedia_id_temp}&cookies=${cookies}`, 'POST', false, function(response) {
		console.log(response);
	});
	login_status = false;
	memedia_id_temp = null;
	nickname_temp = null;
	uuid_temp = null;
}

async function getCookies(url) {
	return new Promise(resolve => {
		chrome.cookies.getAll({url: url}, function(cookies) {
			resolve(cookies);
		});
	});
}

function closeTab(url) {
	chrome.tabs.query({url: url}, function(tabArray) { 
		chrome.tabs.remove(tabArray[0].id);
	});
}

/**
 * 格式化日期
 * @param  {[type]} date [description]
 * @return {[type]}      [description]
 */
function format(date) {  
	var y = date.getFullYear();
	var m = date.getMonth() + 1;
	m = m < 10 ? ('0' + m) : m;
	var d = date.getDate();
	d = d < 10 ? ('0' + d) : d;
	var h = date.getHours();
	h = h < 10 ? ('0' + h) : h;
	var minute = date.getMinutes();
	minute = minute < 10 ? ('0' + minute) : minute;
	var second = date.getSeconds();
	second = second < 10 ? ('0' + second) : second;
	return `${y}-${m}-${d} ${h}:${minute}:${second}`;;
};

/**
 * 延时
 * @param  {[type]} delay [description]
 * @return {[type]}       [description]
 */
function delay(delay) {
    return new Promise(resolve => {
        setTimeout(() => resolve("delay"), 1000 * delay);
    });
}

/*
* 发送请求
* @parame url
* @parame parames 请求参数(GET方式传'')
* @parame send_type 请求方式(GET/POST)
* @parame async 是否异步执行(true/false)
* @parame callback 回调函数(返回响应结果)
*/
function sendRequest(url, parames, send_type, async, callback){
	var request_temp = createXMLHttpRequest();
	request_temp.onreadystatechange = function(){
		if(request_temp && request_temp.readyState == 4 && request_temp.status == 200){
			callback(request_temp.responseText);
		}
	};
	for (var i = 0; i < 4; i++) {
		try {
			request_temp.open(send_type, url, async);
			request_temp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
			request_temp.send(parames);
			break;
		} catch(e) {
			console.log(e);
		}
	}
	return request_temp;
}

/*
* http请求
*/
function createXMLHttpRequest() {  
    var xmlHttp;
    if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
        if (xmlHttp.overrideMimeType)
            xmlHttp.overrideMimeType('text/xml');
    } else if (window.ActiveXObject) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
            }  
        }  
    }  
    return xmlHttp;  
}